﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using RFMobile.Session;
using RFMobile.Database;

namespace RFMobile
{
    public partial class loginScreen : Form
    {
        String titleForm;
        mobileSession mb = mobileSession.Instance;

        public loginScreen()
        {
            InitializeComponent();
            this.ShowDialog();
        }

        private bool isUserValid(String e)
        {
            String[] result = e.Split('@');
            if (result.Length == 2)
            {
                if (result[1].Split('.').Length == 2)
                {
                    result = null;
                    return (true);
                }
            }
            result = null;
            return (false);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            
        }

        private void loginScreen_Load(object sender, EventArgs e)
        {
            DatabaseManager login = new DatabaseManager();
            login.BuildDataSet(new String[] {"mainteiner", "users"}, "mainteinerSet");
            DataRow foundRow = login.Ds.Tables["mainteiner"].Rows.Find(1);
            titleForm = foundRow["company_name"].ToString();
            titleLabel.Text = titleForm;

            DataTable usersTable = login.Ds.Tables["users"];
            foreach (DataRow drCurrent in usersTable.Rows)
            {
                loginComboBox.Items.Add(drCurrent["email"].ToString());
            }

            login.Close();
            login = null;
            usersTable = null;
            foundRow = null;
        }

        private void loginScreen_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void menuItem1_Click_1(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (mb.Login(loginComboBox.SelectedItem.ToString(), passTextBox.Text))
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(mb.name + " " + mb.lastname1 + "\r\n" + mb.position, "Bienvenido");
                mainScreen main = new mainScreen();
                main = null;
            }
            else
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show("Intentelo nuevamente.", "Datos No Validos");
                passTextBox.Text = "";
                mb.Logout();
            }
        }

        private void titleLabel_TextChanged(object sender, EventArgs e)
        {
            if (titleLabel.Text == "")
            {
                titleLabel.Text = titleForm;
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void loginComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}